package info.motteke.kurumi;

import info.motteke.kurumi.swt.SwtAnimationImage;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.GlyphMetrics;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.TextLayout;
import org.eclipse.swt.graphics.TextStyle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

public class AnimationTest {

    public static void main(String[] args) {

        final Display d = Display.getDefault();

        Shell shell = new Shell(d);

        shell.setText("アニメーションテスト");
        shell.setLayout(new FillLayout());
        final Composite c = new Composite(shell, SWT.BORDER);

        final TextLayout layout = new TextLayout(d);

        String text = "ここ\uFFFcとここ\uFFFcに画像が入ります。";
        layout.setText(text);

        AnimationTimer<Image> timer = new AnimationTimer<Image>();

        timer.addListener(new AnimationListener<Image>() {
            @Override
            public void animate() {
                Display.getDefault().asyncExec(new Runnable() {

                    @Override
                    public void run() {
                        c.redraw();
                    }
                });
            }
        });

        final SwtAnimationImage image1 = new SwtAnimationImage(getLoader("246.gif"), 2);
        final SwtAnimationImage image2 = new SwtAnimationImage(getLoader("57.gif"), 6);
        timer.addImage(image1);
        timer.addImage(image2);

        setStyle(layout, image1);
        setStyle(layout, image2);

        c.addListener(SWT.Paint, new Listener(){

            @Override
            public void handleEvent(Event event) {
                Point margin = new Point(10, 10);
                layout.setWidth(c.getClientArea().width - 2 * margin.x);
                layout.draw(event.gc, margin.x, margin.y);
                update(event, image1);
                update(event, image2);
            }

            private void update(Event event, SwtAnimationImage image) {
                int offset = image.getImageOffset();
                int lineIndex = layout.getLineIndex(offset);
                Point margin = new Point(10, 10);
                FontMetrics lineMetrics = layout.getLineMetrics(lineIndex);
                Point point = layout.getLocation(offset, false);
                GlyphMetrics glyphMetrics = layout.getStyle(offset).metrics;
                int x = point.x + margin.x;
                int y = point.y + margin.y + lineMetrics.getAscent() - glyphMetrics.ascent;
                event.gc.drawImage(image.getCurrentImage(), x, y);
            }
        });

        shell.setSize(100, 100);
        shell.open();
        timer.start();

        while(!shell.isDisposed()) {
            if(!d.readAndDispatch()){
                d.sleep();
            }
        }

        timer.stop();
        timer.dispose();
        layout.dispose();
        d.dispose();
    }

    private static void setStyle(TextLayout layout, SwtAnimationImage image) {
        TextStyle imageStyle = new TextStyle(null, null, null);
        imageStyle.metrics = new GlyphMetrics(image.getHeight(), 0, image.getWidth());
        layout.setStyle(imageStyle, image.getImageOffset(), image.getImageOffset());
        System.out.println("インデックス" + image.getImageOffset() + "の画像に、[" +
                           image.getWidth() + "," + image.getHeight() + "]のサイズを設定しました。");
    }

    private static ImageLoader getLoader(String string) {
        ImageLoader loader = new ImageLoader();
        ClassLoader cl = AnimationTest.class.getClassLoader();
        loader.load(cl.getResourceAsStream(string));
        return loader;
    }
}
